package com.mc.common.guava;


import com.google.common.base.Splitter;
import org.junit.Test;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertThat;

public class SplitterTest {

    @Test
    public void testSplit() {
        List<String> result = Splitter.on("#").splitToList("hello#world");
    }
    @Test
    public void testSplitEmpty() {
        List<String> result = Splitter.on("#").omitEmptyStrings().splitToList("hello##world###");
        assertThat(result, notNullValue());
        assertThat(result.get(0), equalTo("hello"));
        assertThat(result.get(1),equalTo("world"));
        result = Splitter.on("|").trimResults().omitEmptyStrings().splitToList("hello | world|||");
        assertThat(result.get(0),equalTo("hello"));
        assertThat(result.get(1),equalTo("world"));

    }

    /**
     * 按照固定长度截取
     */
    @Test
    public void testSplitFixLength() {
        List<String> result = Splitter.fixedLength(3).splitToList("aaabbbcccdd");
        assertThat(result.get(0), equalTo("aaa"));
        assertThat(result.get(3),equalTo("dd"));
        assertThat(result.size(),equalTo(4));

        String str="service_ct01_attribute";
        String[] s = str.split("_");
        if (str.split("_").length==2) {
            String format = MessageFormat.format("{0}.{1}", s[0], s[1]);
            System.out.println(format);
        }
        if (str.split("_").length==3) {
            String format = MessageFormat.format("{0}[{1}].{2}", s[0], s[1], s[2]);
            System.out.println();
        }
    }

    /**
     * 按照#截取多少个
     */
    @Test
    public void testSplitFixSize() {
        List<String> result = Splitter.on("#").splitToList("aaabbbcccdd");
        assertThat(result.get(0), equalTo("aaa"));
        assertThat(result.get(3),equalTo("dd"));
        assertThat(result.size(),equalTo(4));

    }

    @Test
    public void testSplitOnPatternString() {
        List<String> result = Splitter.onPattern("\\|").trimResults().omitEmptyStrings().splitToList("hello | world|||");
        assertThat(result, notNullValue());
        assertThat(result.size(), equalTo(2));
        assertThat(result.get(0), equalTo("hello"));
        assertThat(result.get(1), equalTo("world"));
    }

    @Test
    public void testSplitOnPattern() {
        List<String> result = Splitter.on(Pattern.compile("\\|")).trimResults().omitEmptyStrings().splitToList("hello | world|||");
        assertThat(result, notNullValue());
        assertThat(result.size(), equalTo(2));
        assertThat(result.get(0), equalTo("hello"));
        assertThat(result.get(1), equalTo("world"));
    }


    @Test
    public void testSplitOnSplitToMap() {
        Map<String, String> result = Splitter.on(Pattern.compile("\\|")).trimResults()
                .omitEmptyStrings().withKeyValueSeparator("=").split("hello=HELLO| world=WORLD|||");
        assertThat(result, notNullValue());
        assertThat(result.size(), equalTo(2));
        assertThat(result.get("hello"),equalTo("HELLO"));
        assertThat(result.get("world"),equalTo("WORLD"));

    }



}
